package com.evil.enterprise.controller;

import com.evil.application.api.RemoteApplicationOrderDataService;
import com.evil.application.api.RemoteApplicationOrderService;
import com.evil.application.pojo.dto.form.control.ControlBaseInfoDTO;
import com.evil.application.pojo.dto.order.*;
import com.evil.application.pojo.dto.order.data.OrderRelatedDataMapReqDTO;
import com.evil.application.pojo.dto.process.node.NodeControlReqDTO;
import com.evil.application.pojo.dto.process.node.NodeControlRespDTO;
import com.evil.application.pojo.dto.reporting.data.RefreshAppOrderReqDTO;
import com.evil.common.application.dto.order.OrderPageDataRespDTO;
import com.evil.common.application.dto.order.data.ControlDataDTO;
import com.evil.common.core.entity.QueryModel;
import com.evil.common.core.entity.R;
import com.evil.common.file.constant.OssConstant;
import com.evil.common.file.dto.FileRespDTO;
import com.evil.common.file.dto.UploadReqDTO;
import com.evil.common.file.enums.BusTypeEnum;
import com.evil.common.log.annotation.RequestLogAnnotation;
import com.evil.file.api.RemoteFileService;
import com.evil.formula.api.RemoteFormulaService;
import com.evil.formula.pojo.dto.CalcReqDTO;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 应用订单接口
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/application/order")
public class ApplicationOrderController {

    private final RemoteApplicationOrderService remoteApplicationOrderService;

    private final RemoteApplicationOrderDataService remoteApplicationOrderDataService;

    private final RemoteFormulaService remoteFormulaService;

    private final RemoteFileService remoteFileService;

    /**
     * 重建ES索引
     *
     * @return R
     */
    @PostMapping("/es/index/re-create")
    public R<?> reCreateESIndex() {
        remoteApplicationOrderDataService.reCreateESIndex();
        return R.success();
    }

    /**
     * 删除ES索引
     *
     * @return R
     */
    @PostMapping("/es/index/delete")
    public R<?> deleteESIndex() {
        remoteApplicationOrderDataService.deleteESIndex();
        return R.success();
    }

    /**
     * 刷新ES订单
     *
     * @param orderId orderId
     * @return R
     */
    @PostMapping("/refresh/es-order-id")
    public R<?> refreshEsOrder(@RequestParam("orderId") Long orderId) {
        remoteApplicationOrderDataService.refreshEsOrderId(orderId);
        return R.success();
    }

    /**
     * 刷新ES订单列表
     *
     * @param orderIds orderIds
     * @return R
     */
    @PostMapping("/refresh/es-order-ids")
    public R<?> refreshEsOrders(@NotEmpty @RequestBody Collection<Long> orderIds) {
        remoteApplicationOrderDataService.refreshEsOrderIds(orderIds);
        return R.success();
    }

    /**
     * 刷新ES订单-参数
     *
     * @param refreshAppOrderReqDTO refreshAppOrderReqDTO
     * @return R
     */
    @PostMapping("/refresh/es-orders")
    public R<?> refreshEsOrders(@RequestBody RefreshAppOrderReqDTO refreshAppOrderReqDTO) {
        remoteApplicationOrderDataService.refreshEsOrderIds(refreshAppOrderReqDTO);
        return R.success();
    }

    /**
     * 计算公式
     *
     * @param formula formula
     * @return R
     */
    @PostMapping("/formula/cal-simple")
    public R<?> formula(@RequestParam("formula") @NotBlank(message = "formula not null") String formula) {
        return R.success(remoteFormulaService.calc(new CalcReqDTO(formula)));
    }

    /**
     * 获取应用表单控件基础信息
     *
     * @param orderBaseReqDTO orderPageBaseReqDTO
     * @return R
     */
    @PostMapping("/visible/control")
    public R<List<ControlBaseInfoDTO>> ordersVisibleControlsBase(@Validated @RequestBody OrderBaseReqDTO orderBaseReqDTO) {
        return R.success(remoteApplicationOrderService.ordersVisibleControlsBase(orderBaseReqDTO));
    }

    /**
     * 获取节点控件
     *
     * @param nodeControlReqDTO nodeControlReqDTO
     * @return R
     */
    @PostMapping("/node/control")
    public R<NodeControlRespDTO> nodeControl(@Validated @RequestBody NodeControlReqDTO nodeControlReqDTO) {
        return R.success(remoteApplicationOrderService.nodeControl(nodeControlReqDTO));
    }

    /**
     * 上传订单控件附件
     *
     * @param uploadReqDTO uploadReqDTO
     * @return R
     */
    @PostMapping("/control/file/upload")
    public R<FileRespDTO> uploadOrderControlFile(@Validated UploadReqDTO uploadReqDTO) {
        uploadReqDTO.setType(BusTypeEnum.APPLICATION_ORDER_CONTROL_FILE.getId());
        uploadReqDTO.setAllowContentType(OssConstant.UPLOAD_ALLOW_MIME_TYPE);
        uploadReqDTO.setAllowMaxSize(OssConstant.UPLOAD_FILE_SIZE);
        return R.success(remoteFileService.uploadFile(uploadReqDTO));
    }

    /**
     * 上传订单编辑器控件附件
     *
     * @param uploadReqDTO uploadReqDTO
     * @return R
     */
    @PostMapping("/tinymce/control/file/upload")
    public R<FileRespDTO> uploadOrderTinymceControlFile(@Validated UploadReqDTO uploadReqDTO) {
        uploadReqDTO.setType(BusTypeEnum.APPLICATION_ORDER_TINYMCE_CONTROL_FILE.getId());
        uploadReqDTO.setAllowContentType(OssConstant.UPLOAD_ALLOW_MIME_TYPE);
        uploadReqDTO.setAllowMaxSize(OssConstant.UPLOAD_FILE_SIZE);
        return R.success(remoteFileService.uploadFile(uploadReqDTO));
    }

    /**
     * 上传订单留言附件
     *
     * @param uploadReqDTO uploadReqDTO
     * @return R
     */
    @PostMapping("/message/file/upload")
    public R<FileRespDTO> uploadFormFile(@Validated UploadReqDTO uploadReqDTO) {
        uploadReqDTO.setType(BusTypeEnum.APPLICATION_ORDER_MESSAGE_FILE.getId());
        uploadReqDTO.setAllowContentType(OssConstant.UPLOAD_ALLOW_MIME_TYPE);
        uploadReqDTO.setAllowMaxSize(OssConstant.UPLOAD_FILE_SIZE);
        return R.success(remoteFileService.uploadFile(uploadReqDTO));
    }

    /**
     * 提交应用订单
     *
     * @param pushOrderReqDTO addOrderReqDTO
     * @return R
     */
    @RequestLogAnnotation(isSaveRequestData = false)
    @PostMapping("/push")
    public R<?> pushOrder(@Validated @RequestBody PushOrderReqDTO pushOrderReqDTO) {
        return R.success(remoteApplicationOrderService.pushOrder(pushOrderReqDTO));
    }

    /**
     * 编辑应用订单
     *
     * @param modifyOrderReqDTO modifyOrderReqDTO
     * @return R
     */
    @RequestLogAnnotation(isSaveRequestData = false)
    @PostMapping("/modify")
    public R<?> modifyOrder(@Validated @RequestBody ModifyOrderReqDTO modifyOrderReqDTO) {
        return R.success(remoteApplicationOrderService.modifyOrder(modifyOrderReqDTO));
    }

    /**
     * 批量编辑应用订单
     *
     * @param appOrdersControlDataDTO appOrdersControlDataDTO
     * @return R
     */
    @RequestLogAnnotation(isSaveRequestData = false)
    @PostMapping("/modify-batch")
    public R<?> modifyOrderBatch(@Validated @RequestBody AppOrdersControlDataDTO appOrdersControlDataDTO) {
        remoteApplicationOrderService.modifyOrderBatch(appOrdersControlDataDTO);
        return R.success();
    }

    /**
     * 应用订单流转
     *
     * @param completeOrderReqDTO completeOrderReqDTO
     * @return R
     */
    @RequestLogAnnotation(isSaveRequestData = false)
    @PostMapping("/complete")
    public R<?> completeOrder(@Validated @RequestBody CompleteOrderReqDTO completeOrderReqDTO) {
        remoteApplicationOrderService.completeOrder(completeOrderReqDTO);
        return R.success();
    }

    /**
     * 订单列表
     *
     * @param queryModel queryModel
     * @return R
     */
    @PostMapping("/list")
    public R<PageInfo<OrderPageDataRespDTO>> orderList(@Validated @RequestBody QueryModel<OrderPageReqDTO> queryModel) {
        return R.success(remoteApplicationOrderService.orderList(queryModel));
    }

    /**
     * 订单统计
     *
     * @param orderStatisticsReqDTO orderStatisticsReqDTO
     * @return R
     */
    @PostMapping("/statistics")
    public R<List<OrderStatisticsRespDTO>> orderStatistics(@Validated @RequestBody OrderStatisticsReqDTO orderStatisticsReqDTO) {
        return R.success(remoteApplicationOrderService.orderStatistics(orderStatisticsReqDTO));
    }

    /**
     * 订单详情
     *
     * @param orderDetailReqDTO orderDetailReqDTO
     * @return R
     */
    @PostMapping("/detail")
    public R<OrderDetailRespDTO> orderDetail(@Validated @RequestBody OrderDetailReqDTO orderDetailReqDTO) {
        return R.success(remoteApplicationOrderService.orderDetail(orderDetailReqDTO));
    }

    /**
     * 订单转交
     *
     * @param orderReassignmentReqDTO orderReassignmentReqDTO
     * @return R
     */
    @RequestLogAnnotation(isSaveRequestData = false)
    @PostMapping("/reassignment")
    public R<?> reassignment(@Validated @RequestBody OrderReassignmentReqDTO orderReassignmentReqDTO) {
        remoteApplicationOrderService.reassignment(orderReassignmentReqDTO);
        return R.success();
    }

    /**
     * 删除订单
     *
     * @param deleteOrderReqDTO deleteOrderReqDTO
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/delete")
    public R<?> deleteOrder(@Validated @RequestBody DeleteOrderReqDTO deleteOrderReqDTO) {
        remoteApplicationOrderService.deleteOrder(deleteOrderReqDTO);
        return R.success();
    }

    /**
     * 获取订单指定控件数据map
     *
     * @param queryModel queryModel
     * @return R
     */
    @PostMapping("/data/map")
    public R<Map<String, List<ControlDataDTO>>> orderRelatedDataMap(@Validated @RequestBody QueryModel<OrderRelatedDataMapReqDTO> queryModel) {
        return R.success(remoteApplicationOrderService.orderRelatedDataMap(queryModel));
    }

}
